<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item
              label="品种"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="pz"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.pz"
                dictCode="pz"
                placeholder="请选择品种"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="生育期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="syq"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.syq"
                dictCode="syq"
                placeholder="请选择生育期"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="阶段名"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="jdm"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.jdm"
                dictCode="jdm"
                placeholder="请选择阶段名"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="开始时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="kssj"
            >
              <a-row :gutter="8">
                <a-col :xs="24" :sm="10">
                  <a-select
                    v-model="selectedStartMonth"
                    placeholder="选择月份"
                    @change="updateStartDays"
                  >
                    <a-select-option v-for="month in 12" :key="month" :value="month"
                      >{{ month }}</a-select-option
                    >
                  </a-select>
                </a-col><a-col :xs="24" :sm="2">月</a-col>
                <a-col :xs="24" :sm="10">
                  <a-select v-model="selectedStartDay" placeholder="选择日期">
                    <a-select-option
                      v-for="day in daysInStartMonth"
                      :key="day"
                      :value="day"
                      >{{ day }}</a-select-option
                    >
                  </a-select> 
                </a-col><a-col :xs="24" :sm="2">日</a-col>
              
              </a-row>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item
              label="结束时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="jssj"
            >
              <a-row :gutter="8">
                <a-col :xs="24" :sm="10">
                  <a-select
                    v-model="selectedEndMonth"
                    placeholder="选择月份"
                    @change="updateEndDays"
                  >
                    <a-select-option v-for="month in 12" :key="month" :value="month"
                      >{{ month }}</a-select-option
                    >
                  </a-select>
                </a-col><a-col :xs="24" :sm="2">月</a-col>
                <a-col :xs="24" :sm="10">
                  <a-select v-model="selectedEndDay" placeholder="选择日期">
                    <a-select-option v-for="day in daysInEndMonth" :key="day" :value="day"
                      >{{ day }}</a-select-option
                    >
                  </a-select>
                </a-col><a-col :xs="24" :sm="2">日</a-col>
              </a-row>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="龄期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="lq"
            >
              <a-input v-model="model.lq" placeholder="请输入龄期"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="灌溉措施"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="ggcs"
            >
              <a-input v-model="model.ggcs" placeholder="请输入灌溉措施"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="施肥措施"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="sfcs"
            >
              <a-input v-model="model.sfcs" placeholder="请输入施肥措施"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="植保措施"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="zbcs"
            >
              <a-input v-model="model.zbcs" placeholder="请输入植保措施"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="积温带"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="jwd"
            >
              <a-input v-model="model.jwd" placeholder="请输入积温带"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="图片"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="tp"
            >
              <j-image-upload isMultiple v-model="model.tp"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="农艺建议"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="nyjy"
            >
              <a-input v-model="model.nyjy" placeholder="请输入农艺建议"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from "@/api/manage";
import { validateDuplicateValue } from "@/utils/util";
import moment from "moment";

export default {
  name: "ZwglNyjyForm",
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      selectedStartMonth: null,
      selectedStartDay: null,
      daysInStartMonth: 31,
      selectedEndMonth: null,
      selectedEndDay: null,
      daysInEndMonth: 31,
      model: {
        kssj: "",
        jssj: "",
      },
      // model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        pz: [{ required: true, message: "此处不可为空!" }],
        syq: [{ required: true, message: "此处不可为空!" }],
        jdm: [{ required: true, message: "此处不可为空!" }],
        // kssj: [
        //   { required: true, message: "此处不可为空!" },
        //   {
        //     validator: this.validateDateFormat,
        //     message: "请输入有效的日期格式, 例如:01-01",
        //   },
        // ],
        // jssj: [
        //   { required: true, message: "此处不可为空!" },
        //   {
        //     validator: this.validateDateFormat,
        //     message: "请输入有效的日期格式, 例如:01-01",
        //   },
        // ],
        tp: [{ required: true, message: "此处不可为空!" }],
      },
      url: {
        add: "/znnqjc/zwglNyjy/add",
        edit: "/znnqjc/zwglNyjy/edit",
        queryById: "/znnqjc/zwglNyjy/queryById",
      },
    };
  },
  computed: {
    formDisabled() {
      return this.disabled;
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    updateStartDays() {
      this.daysInStartMonth = this.getDaysInMonth(this.selectedStartMonth);
      if (this.selectedStartDay > this.daysInStartMonth) {
        this.selectedStartDay = null;
      }
      this.updateModel("kssj", this.selectedStartMonth, this.selectedStartDay);
    },
    updateEndDays() {
      this.daysInEndMonth = this.getDaysInMonth(this.selectedEndMonth);
      if (this.selectedEndDay > this.daysInEndMonth) {
        this.selectedEndDay = null;
      }
      this.updateModel("jssj", this.selectedEndMonth, this.selectedEndDay);
    },
    getDaysInMonth(month) {
      const bigMonths = [1, 3, 5, 7, 8, 10, 12];
      const smallMonths = [4, 6, 9, 11];
      if (bigMonths.includes(month)) {
        return 31;
      } else if (smallMonths.includes(month)) {
        return 30;
      } else {
        // Assuming it's not a leap year, February has 28 days.
        // You can add leap year calculation if needed.
        return 28;
      }
    },
    updateModel(prop, month, day) {
      this.model[prop] = month && day ? `${month}-${day}` : "";
    },
    validateDateFormat(rule, value, callback) {
      const dateFormat = /^\d{2}-\d{2}$/;
      if (!value.match(dateFormat)) {
        callback(new Error("请输入有效的日期格式 (例如:01-01)"));
      } else {
        callback();
      }
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
                console.log(record)
      this.selectedStartMonth =this.model.kssj.substr(0, 1)
      this.selectedStartDay = this.model.kssj.substr(2, 1)
      this.selectedEndMonth =this.model.jssj.substr(0, 1)
      this.selectedEndDay = this.model.jssj.substr(2, 1)
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
          } else {
            httpurl += this.url.edit;
            method = "put";
          }
          if (this.selectedStartMonth == null || this.selectedStartDay == null) {
            this.$message.error("开始日期不能为空");
            that.confirmLoading = false;
            return;
          }
          if (this.selectedEndMonth == null || this.selectedEndDay == null) {
            this.$message.error("结束日期不能为空");
            that.confirmLoading = false;
            return;
          }
          // if (this.selectedStartMonth < 10) {
          //   this.selectedStartMonth = "0" + this.selectedStartMonth;
          // }
          // if (this.selectedStartDay < 10) {
          //   this.selectedStartDay = "0" + this.selectedStartDay;
          // }
          // if (this.selectedEndMonth < 10) {
          //   this.selectedEndMonth = "0" + this.selectedEndMonth;
          // }
          // if (this.selectedEndDay < 10) {
          //   this.selectedEndDay = "0" + this.selectedEndDay;
          // }

          if (
            moment(
              "2023" + "-" + this.selectedStartMonth + "-" + this.selectedStartDay
            ).valueOf() >=
            moment(
              "2023" + "-" + this.selectedEndMonth + "-" + this.selectedEndDay
            ).valueOf()
          ) {
            this.$message.error("开始日期不得大于结束日期");
            that.confirmLoading = false;
            return;
          }
          this.model.kssj = this.selectedStartMonth + "-" + this.selectedStartDay;
          this.model.jssj = this.selectedEndMonth + "-" + this.selectedEndDay;

          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit("ok");
              } else {
                that.$message.warning(res.message);
              }
            })
            .finally(() => {
              that.confirmLoading = false;
            });
        }
      });
    },
  },
  watch: {
    // Whenever the selected day changes, update the model
    selectedDay() {
      this.updateModel();
    },
  },
};
</script>
